/**
 * 主题
 */

import {addClass, hasClass, removeClass, getCssVal, setCssVal, updateClass} from './css'

/**
 * 设置全局灰色风格
 */
export function setGray() {
    const el = document.documentElement
    const val = 'gray'
    !hasClass(el, val) && addClass(el, val)
}

/**
 * 取消全局灰色风格
 */
export function cancelGray() {
    const el = document.documentElement
    const val = 'gray'
    hasClass(el, val) && removeClass(el, val)
}

/**
 * 设置全局色弱风格
 */
export function setColorWeakness() {
    const el = document.documentElement
    const val = 'webkit'
    !hasClass(el, val) && addClass(el, val)
}

/**
 * 取消全局色弱风格
 */
export function cancelColorWeakness() {
    const el = document.documentElement
    const val = 'webkit'
    hasClass(el, val) && removeClass(el, val)
}

export function getCssValue(key: string, el = document.documentElement) {
    return getCssVal(key, el);
}

export function setCssValue(key: string, val: any) {
    setCssVal(key, val);
}

/**
 * 计算颜色渐变
 * @param {String} startColor 起始颜色
 * @param {Boolean} dark 是否逐渐加深
 * @param {Number} step 步长
 * @returns{String[]} 颜色数组
 */
export function colorGradual(startColor: string, dark = false, step = 10) {
    const startRGB = hexToRGB(startColor || '#000000');
    const gradientColors: string[] = []

    for (let i = 1; i <= 9; i ++) {
        const currentRGB = dark ? calculateGradientColorDark(startRGB, i / step) : calculateGradientColorLight(startRGB, i / step)
        const currentColor = rgbToHex(currentRGB)
        gradientColors.push(currentColor)
    }

    return gradientColors

    // 将16进制颜色值转换为RGB值
    function hexToRGB(hex: string) {
        const r = Number.parseInt(hex.substring(1, 3), 16)
        const g = Number.parseInt(hex.substring(3, 5), 16)
        const b = Number.parseInt(hex.substring(5, 7), 16)
        return [r, g, b]
    }

    // 将RGB值转换为16进制颜色值
    function rgbToHex(rgb: any) {
        let r = rgb[0].toString(16)
        let g = rgb[1].toString(16)
        let b = rgb[2].toString(16)
        r = r.length === 1 ? `0${r}` : r
        g = g.length === 1 ? `0${g}` : g
        b = b.length === 1 ? `0${b}` : b
        return `#${r}${g}${b}`
    }

    // 计算高亮渐变色
    function calculateGradientColorLight(startRGB: number[], percentage: number) {
        const endRGB = [255, 255, 255] // 结束颜色为白色
        const currentRGB: number[] = []
        for (let i = 0; i < startRGB.length; i++) {
            const difference = endRGB[i] - startRGB[i]
            // currentRGB.push(startRGB[i] + Math.round((difference / 100) * percentage))
            currentRGB.push(Math.floor(difference * percentage + startRGB[i]))
        }
        return currentRGB
    }

    // 计算暗黑渐变色
    function calculateGradientColorDark(startRGB: number[], percentage: number) {
        const endRGB = [0, 0, 0] // 结束颜色为黑色
        const currentRGB: number[] = []
        for (let i = 0; i < startRGB.length; i++) {
            // const difference = endRGB[i] - startRGB[i]
            // currentRGB.push(startRGB[i] + Math.round((difference / 100) * percentage))
            currentRGB.push(Math.floor(startRGB[i] * (1 - percentage)));
        }
        return currentRGB
    }
}

export function setTheme(theme: string, el = document.documentElement) {
    updateClass(el, theme)
}

/**
 * 设置PrimaryColor，会计算生成深、浅色系
 * @param {String} theme primary | warn | danger | error | info
 * @param {String} colorVal 颜色值,如#000000
 * @param {Element} el 元素对象
 */
export function setThemeColor(colorVal: string, theme = 'primary'): void {
    if (colorVal) {
        setCssVal(`--el-color-${theme}`, colorVal)

        const lightColors = colorGradual(colorVal)
        for (let i = 1; i <= lightColors.length; i++) {
            setCssVal(`--el-color-${theme}-light-${i}`, lightColors[i - 1])
        }
        const darkColors = colorGradual(colorVal, true)
        for (let i = 1; i <= darkColors.length; i++) {
            setCssVal(`--el-color-${theme}-dark-${i}`, darkColors[i - 1])
        }
    }
}

export function setFontFamily(value: string) {
    setCssVal('font-family', value)
}

export function replaceClass(el = document.documentElement, old: string, now: string) {
    removeClass(el, old);
    addClass(el, now);
}